package IO;

import IO.FileUtils;

import java.io.IOException;
import java.util.Map;
import java.util.HashMap;



public class ImageManager {
    private Map<String, String> imageMap = new HashMap<>();//创建map对象

    public void downloadImage(String url) throws IOException {
        String fileName = url.substring(url.lastIndexOf("/") + 1);//从 URL 或文件路径中提取文件名
        String savePath = "E:/Java_project/ImageDownloader/images/" + fileName;//写一个下载路径
        FileUtils.saveImage(url, savePath);//调用下载图片的函数
        imageMap.put(fileName, savePath);//将文件名和下载路径写入map中
    }

    public void listImages() {//遍历map表
        imageMap.forEach((fileName, savePath) -> System.out.println(fileName + " -> " + savePath));
    }

    public void searchImage(String name) {
        imageMap.forEach((fileName, savePath) -> {
            if (fileName.equals(name)) {
                System.out.println(savePath);
            } else {
                System.out.println("未查找到该图片信息\n");
            }
        });
    }
}
